﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.DAL.DAHelpers;
using SMS.Entities;
using System.Data;
using System.Data.SqlClient;

namespace SMS.DAL
{
    public class TrackingDA : BaseDA<SaleHistory>
    {
        public override SaleHistory SaveObject(string userName, SaleHistory tObject)
        {
            throw new NotImplementedException();

        }

        public override bool DeleteObject(string userName, SaleHistory tObject)
        {
            throw new NotImplementedException();
        }

        public override List<SaleHistory> GetObjectList(string userName, DateTime fromDate, DateTime toDate)
        {
            throw new NotImplementedException();
        }

        public override List<SaleHistory> SearchObject(string userName, string SearchText)
        {
            throw new NotImplementedException();
        }

        public List<SaleHistory> GetSaleHistory(string username)
        {
            #region "Procedure"
            /*
             CREATE proc USP_SELECT_SALEHISTORY        
AS        
BEGIN      
 SELECT S.[Sale_ID]      
      ,S.[Customer_ID]  
      ,C.Customer_Name      
      ,S.[TotalSellingPrice]  
      ,S.[ActualSelliingPrice]      
      ,S.[BuyingPrice]      
      ,S.[Discount]      
      ,S.[Selling_Date]      
      ,S.[SalesMan]   
      ,sm.SalesMan_Name     
      ,S.[Counter_ID]  
      --,ct.Counter_Name      
      ,S.[Is_Paid]      
      ,S.[Remarks]      
            
      FROM SALEHISTORY S inner join Customer C on S.[Customer_ID]=C.[Customer_ID]  inner join Counter ct  
      on S.[Counter_ID] =ct.[Counter_ID] inner join SalesManDetail sm on sm.SalesMan_ID=S.SalesMan  
      WHERE S.ISLIVE=1         
END  
             */
            #endregion
            System.Data.IDataReader reader = null;
            try
            {
                reader = dbHelper.ExecuteReader("USP_SELECT_SALEHISTORY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                List<SaleHistory> history = new List<SaleHistory>();
                while (reader.Read())
                    history.Add(GetSaleHistoryObject(reader));
                reader.Close();

                return history;
            }
            catch (Exception ex)
            {
                LogException(username, ex, null);
                throw;
            }

        }

        private SaleHistory GetSaleHistoryObject(System.Data.IDataReader reader)
        {
            int fieldcount = reader.FieldCount;

            SaleHistory history = new SaleHistory();
            int i = -1;
            if (!reader.IsDBNull(++i))
                history.Sale_ID = reader.GetInt32(i);
            if (!reader.IsDBNull(++i) && !reader.IsDBNull(++i))
                history.Customer = new Customer { Customer_ID = reader.GetInt32(i - 1), Customer_Name = reader.GetString(i) };
            if (!reader.IsDBNull(++i))
                history.TotalSellingPrice = reader.GetDecimal(i);
            if (!reader.IsDBNull(++i))
                history.ActualSelliingPrice = reader.GetDecimal(i);
            if (!reader.IsDBNull(++i))
                history.BuyingPrice = reader.GetDecimal(i);
            if (!reader.IsDBNull(++i))
                history.Discount = reader.GetDecimal(i);
            if (!reader.IsDBNull(++i))
                history.Selling_Date = reader.GetDateTime(i);
            if (!reader.IsDBNull(++i) && !reader.IsDBNull(++i))
                history.SalesMan = new SalesManDetail { SalesMan_ID = reader.GetInt32(i - 1), SalesMan_Name = reader.GetString(i) };
            if (!reader.IsDBNull(++i))
                history.Counter_ID = reader.GetInt32(i);
            if (!reader.IsDBNull(++i))
                history.Is_Paid = reader.GetBoolean(i);
            if (!reader.IsDBNull(++i))
                history.Remarks = reader.GetString(i);
            return history;
        }

        public List<SaleDetail> GetSaleDetails(string userName, int saleID)
        {

            #region "Procedure"
            /*
             CREATE proc USP_SELECT_SALEDETAIL     
(  
@SaleID int  
)   
AS      
BEGIN    
 SELECT [SaleDetail_ID]    
     ,[Sales_ID]    
     ,[ProductStockWiseID]    
     ,[Quantity]    
     ,[SellingUnitPrice]    
     ,[BilledAmount]    
     ,[TotalAmount]    
     ,[Discount]    
     ,[IsLive]    
          
      FROM SALEDETAIL    
      WHERE Sales_ID=@SaleID and ISLIVE=1       
END
             */
            #endregion
            System.Data.IDataReader reader = null;
            try
            {
                dbHelper.AddParameter("@SaleID", saleID);
                reader = dbHelper.ExecuteReader("USP_SELECT_SALEDETAIL", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                List<SaleDetail> history = new List<SaleDetail>();
                while (reader.Read())
                    history.Add(GetSaleDetailObject(reader));
                reader.Close();

                return history;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, null);
                throw;
            }

        }

        private SaleDetail GetSaleDetailObject(System.Data.IDataReader reader)
        {
            SaleDetail detail = new SaleDetail();
            int i = 0;
            detail.SaleDetail_ID = reader.GetInt32(i++);
            detail.SalesHistory = new SaleHistory { Sale_ID = reader.GetInt32(i++) };
            detail.ProductStockWise = new ProductStockWise { Product_Stock_ID = reader.GetInt32(i++) };
            detail.Quantity = reader.GetInt32(i++);
            detail.SellingUnitPrice = reader.GetDecimal(i++);
            detail.BilledAmount = reader.GetDecimal(i++);
            detail.TotalAmount = reader.GetDecimal(i++);
            detail.Discount = reader.GetDecimal(i++);
            detail.IsLive = reader.GetBoolean(i++);
            return detail;
        }

    }
}
